package com.hejiale.lease.web.admin.schedule;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hejiale.lease.model.entity.LeaseAgreement;
import com.hejiale.lease.model.enums.LeaseStatus;
import com.hejiale.lease.web.admin.service.LeaseAgreementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description: 定时任务类
 * @author: HeJiale
 * @since 2025/3/20 下午12:36
 */
@Component
public class ScheduledTasks {
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Scheduled(cron = "0 0 0 * * *")
    public void checkLeaseStatus() {
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        Date now = new Date();
        updateWrapper.le(LeaseAgreement::getLeaseEndDate, now)
                .in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING)//检查状态为签约和退租待确认的租约
                .set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED);

        leaseAgreementService.update(updateWrapper);
    }
}
